package com.example.lcpractice.lc;

/**
 * 给定一个字符串，验证它是否是回文串，只考虑字母和数字字符，可以忽略字母的大小写。
 * <p>
 * 说明：本题中，我们将空字符串定义为有效的回文串。
 * <p>
 * 示例 1:
 * <p>
 * 输入: "A man, a plan, a canal: Panama"
 * 输出: true
 * 解释："amanaplanacanalpanama" 是回文串
 * 示例 2:
 * <p>
 * 输入: "race a car"
 * 输出: false
 * 解释："raceacar" 不是回文串
 * 提示：
 * <p>
 * 1 <= s.length <= 2 * 105
 * 字符串 s 由 ASCII 字符组成
 *
 * @author hd
 * @date 2022-07-08 09:51
 * @description 回文数
 */
public class Lc125 {


    public static void main(String[] args) {
//        System.out.println("h e l l".replaceAll(" ",""));
//        System.out.println(isLegalChar('9'));
//        System.out.println(isPalindrome("123 + * 321"));
        System.out.println(isPalindrome(".a"));
    }

    // for循环实现
    public static boolean isPalindrome(String s) {
        String s1 = s.toLowerCase();
        String s2 = s1.replaceAll(" ", "");
        StringBuilder builder = new StringBuilder(s2);
        for (int i = builder.length() - 1; i >= 0; i--) {

            if (!Character.isLetterOrDigit(builder.charAt(i))) {
                builder.deleteCharAt(i);
            }
        }
        String strRemain = builder.toString();
        boolean flag = true;
        for (int i = 0; i < strRemain.length() / 2; i++) {
            if (strRemain.charAt(i) != strRemain.charAt(strRemain.length() - 1 - i)) {
                flag = false;
                break;
            }
        }

        return flag;
    }

    //双指针而非for循环
    public static boolean isPalindrome2(String s) {
        StringBuilder sgood = new StringBuilder();
        int length = s.length();
        for (int i = 0; i < length; i++) {
            char ch = s.charAt(i);
            if (Character.isLetterOrDigit(ch)) {
                sgood.append(Character.toLowerCase(ch));
            }
        }
        int n = sgood.length();
        int left = 0, right = n - 1;
        while (left < right) {
            if (Character.toLowerCase(sgood.charAt(left)) != Character.toLowerCase(sgood.charAt(right))) {
                return false;
            }
            ++left;
            --right;
        }
        return true;
    }
    // 双指针 不修改原字符串
    public boolean isPalindrome3(String s) {
        int n = s.length();
        int left = 0, right = n - 1;
        while (left < right) {
            while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
                ++left;
            }
            while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
                --right;
            }
            if (left < right) {
                if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
                    return false;
                }
                ++left;
                --right;
            }
        }
        return true;
    }

    /**
     * （已有api实现此功能）Character.isLetterOrDigit(ch)
     *
     * @param ch 任意字符
     * @return true, 数字或者字母
     */
    public static boolean isLegalChar(Character ch) {
        return ('0' <= ch && '9' >= ch) || ('a' <= ch && 'z' >= ch) || ('A' <= ch && 'Z' >= ch);
    }
}
